<?php

namespace api\modules\app\services;

use api\models\UserInfo;
use Yii;

class UserService
{
    const USE_TYPE_MANGER = 1;
    const USE_TYPE_LIST = 2;
    const USE_TYPE_USER = 3;
    const USE_TYPE_SUP = 4;

    const STATE_ENABELED = 1;
    // 用户所属平台还是部门
    const SERVICE_TYPE_DEPARTMENT = 1;
    const SERVICE_TYPE_PLATFORM = 2;

    public static function getUserById($id)
    {
        return Yii::$app->userCenter->getUserById($id);
    }

    /**
     * 根据 user 拼接 meta_level 和 department 查询条件
     */
    public static function genListUserCondition($user, $perfix = '', $useType = 2)
    {
        if ($user['id'] == 1) {
            return [];
        }

        $info = $user['userInfo'];
        list($level, $type) = explode('-', $info['level']);
        $condition = [];

        switch ($useType) {
            case self::USE_TYPE_SUP:
                for ($i = 0; $i <= $level; $i++) {
                    $condition[(empty($perfix) ? '' : "$perfix.") . 'meta_level' . ($i + 1)] = empty($info["meta_$i"]) ? '0' : $info["meta_$i"];
                }
                break;
            case self::USE_TYPE_MANGER:
                $condition['route'] = ($type == 1) ? $info['department_code'] : $info['meta_' . $level];
                $condition['not meta_id'] = $condition['route'];
                break;
            default:
                $condition = [
                    (empty($perfix) ? '' : "$perfix.") . 'meta_level1' => empty($info['meta_0']) ? '0' : $info['meta_0'],
                    (empty($perfix) ? '' : "$perfix.") . 'meta_level2' => empty($info['meta_1']) ? '0' : $info['meta_1'],
                    (empty($perfix) ? '' : "$perfix.") . 'meta_level3' => empty($info['meta_2']) ? '0' : $info['meta_2'],
                    (empty($perfix) ? '' : "$perfix.") . 'meta_level4' => empty($info['meta_3']) ? '0' : $info['meta_3'],
                    (empty($perfix) ? '' : "$perfix.") . 'meta_level5' => empty($info['meta_4']) ? '0' : $info['meta_4'],
                    (empty($perfix) ? '' : "$perfix.") . 'department' => empty($type) ? '0' : (empty($info['department_code']) ? '0' : $info['department_code']),
                    (empty($perfix) ? '' : "$perfix.") . 'user_id' => '0',
                ];
                if ($useType == self::USE_TYPE_USER) { // 个人
                    $condition[(empty($perfix) ? '' : "$perfix.") . 'user_id'] = $user['id'];
                }
                break;
        }

        // // 用户有平台和部门两个权限，则不增加限制条件
        // if ($info['organization_service'] && in_array(self::SERVICE_TYPE_DEPARTMENT, $info['organization_service']) && in_array(self::SERVICE_TYPE_PLATFORM, $info['organization_service'])) {
        //     unset($condition[(empty($perfix) ? '' : "$perfix.") . 'department']);
        // }

        return $condition;
    }

    public static function genDelayListCondition($user)
    {
        if ($user['id'] == 1) {
            return [];
        }

        $info = $user['userInfo'];
        list($level, $type) = explode('-', $info['level']);
        $condition = [
            'event_dispatch_tree.area_code' => $info['meta_' . $level],
            'event_dispatch_tree.level' => $level,
            'event_dispatch_tree.type' => $type ? 2 : 0,
        ];
        $condition['event_dispatch_tree.department'] = $type ? $info['department_code'] : '0';

        return $condition;
    }

    public static function getDetailInfo($userId)
    {
        $user = UserInfo::findOne(['user_id' => $userId]);

        return [
            'name' => $user->name,
            'meta_level1_name' => '1',
            'meta_level2_name' => '2',
            'meta_level3_name' => '3',
            'meta_level4_name' => '4',
            'meta_level5_name' => '5',
        ];
    }

    /**
     * @param array $user token
     */
    public static function getUserPlatformCondition($user)
    {
        $info = $user['userInfo'];
        list($level, $type) = explode('-', $info['level']);
        $condition = [
            'area_code' => $info['meta_' . $level],
            'level' => $level,
        ];
        if ($type) {
            $condition['type'] = $type ? 1 : 0;
        }
        return $condition;
    }

    /**
     * TODO：返回层级
     */
    public static function getUserLevelInfo($userId)
    {
        return [
            'platform' => 1,
            'mediaLevel' => 2,
        ];
    }
}
